
jQuery.fn.FillOptions = function(url,options){
	var text;
	var value;
	var type;
	var selected;
	var selectValue;
	var keep;
    if(url.length == 0) throw "request is required";        
    text = options.textfield || "text";
    value = options.valuefiled || "value";    
    type = options.datatype.toLowerCase() || "json";
    if(type != "xml")type="json";
    keep = options.keepold?true:false;
    selected = options.selectedindex || 0;
    selectValue =options.defValue;
    $.ajaxSetup({async:false});
    var datas;
    if(type == "xml")
    {
        $.get(url,function(xml){datas=xml;});            	
    }
    else
    {
        $.getJSON(url,function(json){
        	datas=json;
        	
        });
    }
    if(datas == undefined)
    {
		return;
	}
    this.each(function(){
    if(this.tagName == "SELECT"){
            var select = this;
            if(!keep)$(select).html("");
            addOptions(select,datas,value,selected,selectValue,type,text);
        }
    });
}


function addOptions(select,datas,value,selected,selectValue,type,text)
{        
    var options;
    var datas;
    if(type == "xml")
    {
        $(text,datas).each(function(i){            
            option = new Option($(this).text(),$($(value,datas)[i]).text());
            if(selectValue!=null){
            	if($(value,datas)[i]==selectValue)option.selected=true;
        	}else{
        		if(i==selected)option.selected=true;
        	}
            select.options.add(option);
        });
    }
    else
    {
        $.each(datas,function(i,n){
            option = new Option(eval("n."+text),eval("n."+value));
            if(selectValue!=null){
            	if(eval("n."+value)==selectValue)option.selected=true;
	    	}else{
	    		if(i==selected)option.selected=true;
	    	}
            select.options.add(option);
        });
    }
}